﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmNewFranchisor : Form
    {
        public frmNewFranchisor()
        {
            InitializeComponent();
        }

        private void frmNewFranchisor_Load(object sender, EventArgs e)
        {

        }

        #region FranchiseRegisterTab
        private void FillDistributorDropDown()
        {
            //Fill the DataGrid
            List<Distributor> LvList = DistributorDAO.GetAllDistributors();
            DataTable LvTable = new DataTable();

            LvTable.Columns.Add("DistributorID");
            LvTable.Columns.Add("DistributorName");



            foreach (Distributor LvDistributor in LvList)
            {

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvDistributor.Dis_ID.ToString();

                txtDistributorId.AutoCompleteCustomSource.Add(LvDistributor.Dis_ID.ToString());

            }

        }

        private void FillDistributorName()
        {

            List<Distributor> LvList = DistributorDAO.GetAllDistributors(txtDistributorId.Text.ToString());


            foreach (Distributor LvDistributor in LvList)
            {

                lblDistributorName.Text = LvDistributor.Giv_Name.ToString();
            }


        }

        private void txtDistributorId_TextChanged(object sender, EventArgs e)
        {
            FillDistributorDropDown();
        }

        private void txtDistributorId_Enter(object sender, EventArgs e)
        {
            FillDistributorName();
        }

        private void btnFrn_New_Click(object sender, EventArgs e)
        {
            txtFranchisorId.Text = FranchiseDAO.GetNewFranchiserId().ToString();
        }

        private void txtDistributorId_TextChanged_1(object sender, EventArgs e)
        {
            FillDistributorDropDown();
        }

        private void txtDistributorId_Enter_1(object sender, EventArgs e)
        {
            FillDistributorName();
        }

        private void btnFrn_New_Click_1(object sender, EventArgs e)
        {
            txtFranchisorId.Text = FranchiseDAO.GetNewFranchiserId().ToString();
        }

        private int SaveFranchiser()
        {


            Franchise LvFrn = CollectFranchiserFigures();
            if (LvFrn != null)
            {
                return FranchiseDAO.SaveFranchise(LvFrn);
            }
            else
            {
                return 0;
            }
        }

        private Franchise CollectFranchiserFigures()
        {

            try
            {

                Franchise LvFrn = new Franchise();

                LvFrn.F_Id = txtFranchisorId.Text.ToString();
                LvFrn.Dis_Id = txtDistributorId.Text.ToString();
                LvFrn.Shop_Name = txtname.Text.ToString();
                LvFrn.Location = txtFrn_Location.Text.ToString();
                LvFrn.Description = rtxtFrn_Desc.Text.ToString();
                LvFrn.User_Create = "1";
                LvFrn.Date_Create = DateTime.Now;
                LvFrn.Last_Modify = DateTime.Now;


                return LvFrn;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnFrn_Save_Click(object sender, EventArgs e)
        {
            SaveFranchiser();
            MessageBox.Show("NEW FRANCHISE WAS BSUCCESFULLY SAVED");
        }

        private void btnFrn_Clear_Click(object sender, EventArgs e)
        {
            txtFranchisorId.Text = "";
            txtFrn_Location.Text = "";
            txtDistributorId.Text = "";
            rtxtFrn_Desc.Text = "";
            txtname.Text = "";
        }

        #endregion

        private void btnFrn_New_Click_2(object sender, EventArgs e)
        {
            txtFranchisorId.Text = FranchiseDAO.GetNewFranchiserId().ToString();
        }

        private void btnFrn_Save_Click_1(object sender, EventArgs e)
        {
            SaveFranchiser();
            MessageBox.Show("NEW FRANCHISE WAS BSUCCESFULLY SAVED");
        }
    }
}
